#!/bin/sh

detect_tools() {
  for tool in $@ ; do
    echo "$tool=$(which $tool)"
  done
}


gen_clean() {
  stub
}
add_input_action() {
  while [ ! $# = 0 ] ; do
    argument="$1"
    shift
    #old_ifs=$IFS
   # IFS=:
    #for arg in $argument ; do
      tmp_action="$(read_farray "$argument" 1 )"
      tmp_command="$(read_farray "$argument" 2)"
      tmp_help="$(read_farray "$argument" 3 )"
      if [ -z "$input_actions" ] ; then 
	input_actions="${tmp_action:-\:}"
      else
	input_actions="$input_actions:${tmp_action:-\:}"
      fi
      if [ -z "$input_commands" ] ; then
	input_commands="${tmp_command:-\:}"
      else
	input_commands="$input_commands:${tmp_command:-\:}"
      fi

      if [ -z "$input_help" ] ; then
	input_help="${tmp_help:-\-}"
      else
	input_help="$input_help:${tmp_help:-\-}"
      fi
#       input_commands="$input_commands:
#       input_help="$input_help:
  #  done
   # IFS=$old_ifs
  done
  input_parser=true
}



gen_input_parser() {
  if [ ! "$input_not_required" ] ; then
    echo 'if [ ! $# = 0 ] ; then'
  fi
  echo '  eval set -- "$@"'
  echo '  while [ ! $# = 0 ] ; do '
  echo '     case $1 in'
  input_actions_count=$( get_farray_lenght "$input_actions" )
  while [  ! ${counter:-1} = $input_actions_count ] ; do
    counter=$(( $counter + 1 ))
    cached_action="$(read_farray "$input_actions" $counter )"
    if [ ! -z "$cached_action" ] ; then
      echo "         $cached_action) $(read_farray "$input_commands" $counter); shift $shiftnumber;; "
    fi
  done
      echo '         --) shift;  break;;'
  echo '      esac'
  echo '   done'
  if [ ! "$input_not_required" ] ; then
   current_line_ued=$(( $current_line_ued + 1 )) echo 'else'
    echo '   echo "No options given run $0 --help for help"'
    echo '   exit 1'
    echo 'fi'
  fi
  unset counter shiftnumber
}

gen_help() {
  echo "echo "\$INSTALL_APPNAME - help ""
  if [  -z $HELP_DISABLE_DEFAULTS ] ; then
    printf 'cat <<STD_HELP_TEXT'
    printf 'standart options:\n'
    printf '\t--help\tshow this message\n' 
    printf '\t--version -v\t show version of $appname\n'
    printf '\n'
    printf 'STD_HELP_TEXT\n'
 fi
  input_actions_count=$( get_farray_lenght "$input_actions" )
  printf 'cat <<HELP_TEXT\n'
  printf 'options:\n'
  while [  ! ${counter:-1} = $input_actions_count ] ; do
    counter=$(( $counter + 1 ))
    printf "\t$(read_farray "$input_actions" $counter )\t$(read_farray "$input_help" $counter)"
  done
   printf 	'HELP_TEXT\n' 
}

after_parse() {
  if [ x"$input_parser" = xtrue ] ; then	
      if [ ! -z "$input_help" ] ; then
	cat  <<AFTER_PARSE_TEXT_HELP
display_help()
{
$(gen_help)
}
AFTER_PARSE_TEXT_HELP

	input_actions="$input_actions:--help"
	input_commands="$input_commands:display_help;exit"
	input_actions="$input_actions:--version"
	input_commands="$input_commands:echo \$install_sh_ver;exit"
      fi
    gen_input_parser
  fi
}
